package com.channel.temperature.collect.ui.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.channel.temperature.collect.data.repository.UserRepository
import com.channel.temperature.collect.data.response.testresponse.Player
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import me.hgj.mvvmhelper.util.XLog

class MainViewModel() : ViewModel() {
    val userLiveData = MutableLiveData<Player>()
    private val userRespository = UserRepository
    fun getuser(count: Int) {
        viewModelScope.launch {
            userLiveData.value = userRespository.getUser(count)
        }
    }

    suspend fun getInfo1(): Boolean = suspendCancellableCoroutine {
        viewModelScope.launch {
            delay(5000)
            XLog.e("2秒后执行这里")
        }
    }

    fun run5() {
        var result = "sasasasassasas"
//        CoroutineScope(Dispatchers.IO).launch {
//            delay(3000)
//            XLog.e("执行了代码")
//        }
        CoroutineScope(Dispatchers.Main).launch {
            val task = withContext(Dispatchers.IO) {
                delay(3000)
                "这是我的最后返回数据"
            }
            XLog.e("开始执行111111111" + task)
            result = task
        }

    }
}